// JavaScript Document
/**
 * @description    : 瀑布流
 * @author         : laohu@staff.sina.com.cn
 * @create date    : 2012-03-15
 * @change date    :  
 * @change details : 
 * @parameter      : 
 * @details        : 
 * @return         :  */

(function(){
	
	if (!Array.indexOf) Array.prototype.indexOf = function(b) {
		for (var a = 0; a < this.length; a++) if (this[a] == b) return a;
		return - 1
	};
	
	function WaterFall(){}	
	WaterFall.prototype = {
		constructor : WaterFall,
		init : function( options ){
			var self = this;
			self.options = $.extend( {
				icontainerId : "imagewall_container",  //内容  ID
				loadingId : "iload",  //loading  ID
				pageId : "",    //分页ID
				url :  "/interface/pubu/json_get_pubu_data_by_type.php?callback=jsonp1333795092960&t=jsonp",       //获取数据地址
				scrollFixed : 100,
				pscreen : 8,
				start_value : 0,
				isRandomComment : false,
				callback : function(){}
			}, options || {} );
			self.icontainer = $("#" + self.options.icontainerId);
			self.iload = $("#" + self.options.loadingId);
			self.iload.before("<div id='___h' style='height:0;overflow:hidden;'></div>");
			self.iheight = $("#___h");
			//self.ipage = $("#" + self.options.pageId);
			self.loading = false;
			self.times = self.options.pscreen;
			self.count = self.options.pscreen + 1;
			self.start_value =  self.options.start_value;
			self.firstLoad = 0;
			self.isOver = false;
			self.timer = null;
			self.$page = self.options.pageId == '' ? null : $("#" + self.options.pageId);
			self.initData();			
		},
		initData : function(){
			var self = this,
				options = self.options,
				sub_c = self.icontainer.find(".col");
			self.sub_c = sub_c;
			self.sub = [];
			self.dis = [];
			//self.data = [];
			for(var i = 0, len = sub_c.size(), e = null; i < len; i++){
				e = sub_c.eq(i);
				self.sub.push(e);	
			}	
			self._handleData();
			self._calDis();
			self.execute();
			if(self.$page){
				self.$page.hide();	
			}
		},
		_calDis : function(){
			var self = this;
			self.dis = [];
			for(var i = 0, len = self.sub_c.size(), e = null; i < len; i++){
				e = self.sub_c.eq(i);
				//self.dis.push(e.height() + e.offset().top);
				self.dis.push(e.height());
			}				
		},
		_loadData : function(paramObj, url, callback){               //ajax加载数据

			var self = this;
			paramObj.t = "json";
			$.ajax({
				url : url,
				data : paramObj,
				dataType : 'json',
				success : function(data){
					if(parseInt(data.error, 10) === 0)
					{
						callback(data);
					}else{
						self._tip(data.errmsg);
					}					
				}
			});				
		},
		_tip : function(message){           //提示工具
			try{
				DFZ.APP.warn( message );
			}catch(e){
				alert(message);
			}				
		},	
		_renderData : function(data){
			var self = this,
				options = self.options,
				len = data.length;
			
				//len = self.data.length;
			if(len == 0){
				self.loading = false;				
				//self.iload.stop().fadeOut("slow");
				
				self.iload.hide();
				
				//随机显示评论
				//if(!options.isRandomComment){
				//	var $li = self.icontainer.find("li"),
				//		n = $li.size();
				//	if( DFZ.APP.FOOD.DialogLogin.hasLogin() && n >= 2){
				//		$li.eq(n - 2).find("a.imp").trigger("trile");	
				//	}
				//}
				
				//console.log(self.times + ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
				
				if(self.times === -1){
					self.isOver = true;	
				}			
				return;
			}else{
				//var c = $(self.data.shift());

				var c = $(data.shift());
				var e = self.dis.indexOf(Math.min.apply(Math, self.dis));
				
				self.sub[e].append(c);
				self._calDis();
				setTimeout(function(){
					self._renderData(data);
				}, 0);
			}								
		},
		_handleData : function(){
			var self = this,
				options = self.options;
			self._loadData( {
				t : 'jsonp',
				start_value:self.start_value,
				pscreen : self.count - self.times
			}, options.url, function(data){
				
				//self.data =  data.data;
				self._renderData(data.data);
				if(data.start_value)
				{
					self.start_value = data.start_value;
				}
				if(data.is_finish){
					self.isOver = true;
					if(self.$page){
						self.$page.show();	
					}
					options.callback();
				}
				self.firstLoad++;
			} );
			self.times--;	
			
		},
		execute : function(){			
			var self = this;
			if(self.timer){
				clearTimeout(self.timer);	
			}
			
			var	options = self.options,
				$window = $(window),
				$document = $(document),
				d = $window.scrollTop() + $window.height(),
				c = self.iheight.offset().top - options.scrollFixed;	
				
			if( self.firstLoad > 0 && !self.loading && !self.isOver && (d > c ) ){
				self.iload.show();
				self.loading = true;
				setTimeout(function(){
					self._handleData();
				}, 100);				
			}
			
			!self.isOver && (self.timer = setTimeout(function(){
				self.execute();	
			}, 100));											
		}
	};
	

YOUXIXIU.MAIN.WATERFALL = function(o){
	var p = new WaterFall();
	p.init(o);
	return p;
};

 
})(jQuery);

